# Copyright 1999-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

DESCRIPTION="Google Web Toolkit"
HOMEPAGE="http://code.google.com/webtoolkit/"
SRC_URI="gwt-${PV}.tar.bz2
		 gwt-tools.tar.bz2
		 http://www.apache.org/dist/tapestry/tapestry-4.0.2.tar.gz"

LICENSE="Apache-2.0"
SLOT="0"
KEYWORDS="~x86"

IUSE="doc examples"

COMMON_DEP=">=virtual/jre-1.4"

RDEPEND="${COMMON_DEP}"

DEPEND=">=virtual/jdk-1.4
		>=dev-java/ant-1.6.5
		>=dev-java/junit-3.8.1
		dev-java/ant-contrib
		=dev-java/xerces-2.3.0-r1
		>=dev-util/checkstyle-4.2
		dev-java/mx4j-core
		dev-java/commons-el
		dev-java/commons-digester
		dev-java/commons-modeler
		<www-servers/tomcat-5.5
		<dev-java/tomcat-servlet-api-6.0
		~dev-java/eclipse-ecj-3.1
		${COMMON_DEP}"

# TODO: Additional dependency when using system swt: "<dev-java/swt-3.2.2"

### Custom functions ###
function getJAR() {
	CP=":`java-config -p $1`:"
	JAR=`echo "$CP" | sed -n "s|.*:\(.*/${2}\):.*|\1|p"`
	[ -z "$JAR" ] && die "Something missing in dependencies -- We need $2 from package $1"
	echo "$JAR"
}
function replaceJAR() {
	sed -i "s|\".*/${1}\"|\"`getJAR $2 $3`\"|" "$4"
}

### Custom constants ###
number="[[:digit:]]\+"
version="${number}\(\.${number}\)*"

### Custom variables ###
export GWT_VERSION="${PV}-gentoo-${PR}"

src_compile() {
	cd "${WORKDIR}"

	sed -i 's|depends="linux, windows, mac"|depends="linux"|' gwt/platforms.ant.xml

	replaceJAR "ant-${version}\.jar" ant-core ant.jar gwt/dev/core/build.xml
	replaceJAR "ant-launcher-${version}\.jar" ant-core ant-launcher.jar gwt/dev/core/build.xml
	replaceJAR "junit-${version}\.jar" junit junit.jar gwt/dev/core/build.xml
	replaceJAR "junit-${version}\.jar" junit junit.jar gwt/common.ant.xml
	replaceJAR "junit-${version}\.jar" junit junit.jar gwt/user/build.xml
	replaceJAR "xerces-${version}\.jar" xerces-2.3 xercesImpl.jar gwt/dev/core/build.xml
	replaceJAR "xml-apis-${version}\.jar" xerces-2.3 xml-apis.jar gwt/dev/core/build.xml
	replaceJAR "ant-junit-${version}\.jar" ant-tasks ant-junit.jar gwt/common.ant.xml
	replaceJAR "ant-contrib-${version}\.jar" ant-contrib ant-contrib.jar gwt/common.ant.xml
	replaceJAR "mx4j-jmx-${version}\.jar" mx4j-core-3.0 mx4j-jmx.jar gwt/dev/core/build.xml
	replaceJAR "commons-el-${version}\.jar" commons-el commons-el.jar gwt/dev/core/build.xml
	replaceJAR "commons-digester-${version}\.jar" commons-digester commons-digester.jar gwt/dev/core/build.xml
	replaceJAR "commons-logging-${version}\.jar" commons-logging commons-logging.jar gwt/dev/core/build.xml
	replaceJAR "commons-beanutils-${version}\.jar" commons-beanutils-1.6 commons-beanutils.jar gwt/dev/core/build.xml
	replaceJAR "commons-collections-${version}\.jar" commons-collections commons-collections.jar gwt/dev/core/build.xml
	replaceJAR "commons-modeler-${version}\.jar" commons-modeler commons-modeler.jar gwt/dev/core/build.xml
	replaceJAR "jakarta-regexp-${version}\.jar" jakarta-regexp-1.3 jakarta-regexp.jar gwt/dev/core/build.xml
	replaceJAR "catalina-optional-${version}\.jar" tomcat-5 catalina-optional.jar gwt/dev/core/build.xml
	replaceJAR "tomcat-coyote-${version}\.jar" tomcat-5 tomcat-coyote.jar gwt/dev/core/build.xml
	replaceJAR "tomcat-util-${version}\.jar" tomcat-5 tomcat-util.jar gwt/dev/core/build.xml
	replaceJAR "naming-factory-${version}\.jar" tomcat-5 naming-factory.jar gwt/dev/core/build.xml
	replaceJAR "naming-resources-${version}\.jar" tomcat-5 naming-resources.jar gwt/dev/core/build.xml
	replaceJAR "servlets-default-${version}\.jar" tomcat-5 servlets-default.jar gwt/dev/core/build.xml
	replaceJAR "servlets-invoker-${version}\.jar" tomcat-5 servlets-invoker.jar gwt/dev/core/build.xml
	replaceJAR "catalina-${version}\.jar" tomcat-5 catalina.jar gwt/dev/core/build.xml
	replaceJAR "jasper-compiler-${version}\.jar" tomcat-5 jasper-compiler.jar gwt/dev/core/build.xml
	replaceJAR "jasper-runtime-${version}\.jar" tomcat-5 jasper-runtime.jar gwt/dev/core/build.xml
	replaceJAR "tomcat-http11-${version}\.jar" tomcat-5 tomcat-http11.jar gwt/dev/core/build.xml
	replaceJAR "tomcat-jk2-${version}\.jar" tomcat-5 tomcat-jk2.jar gwt/dev/core/build.xml
	replaceJAR "naming-common-${version}\.jar" tomcat-5 naming-common.jar gwt/dev/core/build.xml
	replaceJAR "naming-java-${version}\.jar" tomcat-5 naming-java.jar gwt/dev/core/build.xml
	replaceJAR "servlets-common-${version}\.jar" tomcat-5 servlets-common.jar gwt/dev/core/build.xml
	replaceJAR "servlet-api-${version}\.jar" tomcat-servlet-api-2.4 servlet-api.jar gwt/dev/core/build.xml
	replaceJAR "servlet-api-${version}\.jar" tomcat-servlet-api-2.4 servlet-api.jar gwt/user/build.xml
	replaceJAR "jsp-api-${version}\.jar" tomcat-servlet-api-2.4 jsp-api.jar gwt/dev/core/build.xml
	replaceJAR "org\.eclipse\.swt\.gtk-linux-${version}\.jar" swt-3 swt.jar gwt/dev/core/build.xml
	replaceJAR "\${gwt\.dev\.swt\.jar}" swt-3 swt.jar gwt/dev/common.ant.xml
	replaceJAR "jdt-${version}\.jar" eclipse-ecj-3.1 ecj.jar gwt/dev/core/build.xml

	# Quick build of the few classes from Apache Tapestry that GWT needs
	cd tapestry-*/framework/src/java/
	CLASSES=org/apache/tapestry/util/text
	javac "${CLASSES}"/*.java
	rm "${CLASSES}"/*.java
	jar cf "${WORKDIR}/temp.jar" "${CLASSES}"
	cd "${WORKDIR}"
	sed -i "s|\".*/tapestry-util-text-${version}\.jar\"|\"${WORKDIR}/temp.jar\"|" gwt/dev/core/build.xml

	# *** SWT TESTING (doesn't work yet) ***
	# TODO: google-web-toolkit currently uses a prebuilt libgwt-ll.so. Can we build it ourselves (in gwt/jni/linux) ?
	# 1 - Use system swt instead of bundled one
	#sed -i "s|\(System\.setProperty(\"swt\.library\.path\",\).*);|\1\"/usr/lib/\");|" gwt/dev/core/src/com/google/gwt/util/tools/ToolBase.java
	# 2 - Use system browser instead of bundled one
	#BROWSER_DIR="/usr/lib/seamonkey"
	#sed -i "s|return installPath + \"/mozilla-${version}\";|return \"${BROWSER_DIR}\";|" gwt/dev/linux/src/com/google/gwt/dev/shell/moz/LowLevelMoz.java

	jar1=`getJAR checkstyle checkstyle.jar`
	jar2=`getJAR commons-beanutils-1.6 commons-beanutils.jar`
	sed -i "s|\".*/checkstyle-all-${version}\.jar;|\"${jar1};${jar2};|" gwt/common.ant.xml

	path1="<pathelement location=\"${jar1}\"/>"
	path2="<pathelement location=\"${jar2}\"/>"
	sed -i "s|<pathelement location=\".*/checkstyle-all-${version}\.jar\" />|${path1}${path2}|" gwt/build-tools/customchecks/build.xml

	# Keep only the tools/ files we need. Not necessary for successful build,
	# but allows us to be certain which files we actually need for the build.
	mv tools tools-full
	mkdir -p tools/lib/eclipse tools/redist
	mv tools-full/lib/eclipse/libswt-*gtk-*.so tools/lib/eclipse/
	mv tools-full/redist/mozilla tools/redist/
	rm -R tools-full

	cd gwt
	ant || die "The ant build failed."
}

src_install() {
	MY_W="${WORKDIR}/gwt/build/staging/gwt-linux-${GWT_VERSION}"

	# Perform a few modifications to the file tree
	mv -- "${MY_W}"/mozilla-*/COPYING "${MY_W}/mozilla-COPYING"
	for DOC in about.html about.txt COPYING COPYING.html index.html mozilla-COPYING release_notes.html; do
		dodoc "${MY_W}/${DOC}"
		rm -- "${MY_W}/${DOC}"
	done

	# TODO: patch ant build script to speedup compilation depending on these USE flags ?
	use doc      || rm -R -- "${MY_W}/doc"
	use examples || rm -R -- "${MY_W}/samples"

	dodir "/opt/${PN}"
	cp -R "${MY_W}"/* "${D}/opt/${PN}"
}
